package com.xj0927.springcloud.controller;

import com.xj0927.springcloud.entities.CommonResult;
import com.xj0927.springcloud.entities.Payment;
import com.xj0927.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xj0927
 * @Description:
 * @Date Created in 2020-10-22 17:01
 * @Modified By:
 */
@RestController
@Slf4j  //使用日志
public class PaymentController {
    @Autowired
    private PaymentService paymentService; //注意这里是个接口

    @Value("${server.port}")
    private String serverPort;

    @PostMapping(value = "/payment/create") //使用特定的添加请求【望文知意】
    public CommonResult create(@RequestBody Payment payment){  //注意不要忘记 @RequestBody
        int result = paymentService.create(payment);

        log.info("***插入结果：" + result);//生产中使用日志来记录操作的结果

        if(result > 0){
            return new CommonResult(200,"插入数据库成功,serverPort: " + serverPort,result);
        }else {
            return new CommonResult(444,"插入数据失败",null);
        }
    }


    @GetMapping(value = "/payment/get/{id}") //使用特定的添加请求【望文知意】
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("***查询结果：" + payment);//生产中使用日志来记录操作的结果

        if(payment != null){
            return new CommonResult(200,"查找成功,serverPort:" + serverPort,payment);
        }else {
            return new CommonResult(444,"查找失败，查询id " + id,null);
        }
    }

    @GetMapping("/payment/lb")
    public String getPaymentLb(){
        return serverPort;
    }

}
